package com.jawmask.android.discover.bean.task;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.jawmask.android.discover.bean.GPSLocation;
import com.jawmask.android.discover.bean.SearchRequest;
import com.jawmask.android.discover.bean.SearchResult;
import com.jawmask.android.discover.utils.AppConstants.GOOGLE_SEARCH_PLACE_STATUS;
import com.jawmask.android.discover.utils.AppConstants.HTTP_URL;
import com.jawmask.android.discover.utils.UserSettings;

public class PlaceSearchTask extends BaseDiscoverTask<SearchRequest, Void, List<SearchResult>> {

	public PlaceSearchTask(Context context) {
		super(context);
	}

	@Override
	protected URI getRequestURI(SearchRequest... searchRequests) throws URISyntaxException {
		URI requestUrl = URI.create(HTTP_URL.GOOGLE_SEARCH_PLACE_API_URL.httpUrlValue);
		SearchRequest searchRequest = searchRequests[0];

		UserSettings userSettings = UserSettings.getInstance();
		GPSLocation userLocation = searchRequest.getUserLocation();

		List<NameValuePair> requestParameter = new ArrayList<NameValuePair>();
		requestParameter.add(new BasicNameValuePair("key", getGoogleAppKey()));
		requestParameter.add(new BasicNameValuePair("location", userLocation.getLatitude() + "," + userLocation.getLongitude()));
		requestParameter.add(new BasicNameValuePair("radius", String.valueOf(userSettings.getDistanceInMeter())));
		requestParameter.add(new BasicNameValuePair("sensor", String.valueOf(searchRequest.isLocationFromDevice())));

		requestParameter.add(new BasicNameValuePair("language", userSettings.getUserSelectedLanguage()));
		requestParameter.add(new BasicNameValuePair("keyword", searchRequest.getUserSearchText()));
		if (searchRequest.getPageToken() != null)
			requestParameter.add(new BasicNameValuePair("pagetoken", searchRequest.getUserSearchText()));

		return URIUtils.createURI(requestUrl.getScheme(), requestUrl.getAuthority(), -1, requestUrl.getPath(), URLEncodedUtils.format(requestParameter, HTTP.UTF_8), null);
	}

	protected List<SearchResult> getObjectFromJSON(JSONObject searchResultsJson) throws JSONException {
		String status = searchResultsJson.getString("status");

		if (status.equalsIgnoreCase(GOOGLE_SEARCH_PLACE_STATUS.OK.status)) {
			JSONArray searchResultArray = searchResultsJson.getJSONArray("results");

			List<SearchResult> searchResultList = new ArrayList<SearchResult>();

			SearchResult searchResult = null;
			GPSLocation gpsLocation = null;

			JSONObject searchResultJson = null;
			JSONObject geometryJsonObject = null;
			JSONObject locationJsonObject = null;

			for (int i = 0; i < searchResultArray.length(); i++) {
				searchResult = new SearchResult();
				searchResultJson = searchResultArray.getJSONObject(i);

				if (i == 0)
					searchResult.setNextPageToken(searchResultJson.optString("next_page_token"));

				geometryJsonObject = searchResultJson.getJSONObject("geometry");
				locationJsonObject = geometryJsonObject.getJSONObject("location");

				gpsLocation = new GPSLocation();
				gpsLocation.setLatitude(locationJsonObject.optDouble("lat"));
				gpsLocation.setLongitude(locationJsonObject.optDouble("lng"));
				searchResult.setLocation(gpsLocation);

				searchResult.setIconPath(searchResultJson.optString("icon"));
				searchResult.setIconPath(searchResultJson.optString("icon"));
				searchResult.setResultId(searchResultJson.optString("id"));
				searchResult.setName(searchResultJson.optString("name"));
				searchResult.setAddress(searchResultJson.optString("vicinity"));
				searchResult.setReference(searchResultJson.optString("reference"));
				searchResult.setRating((int) searchResultJson.optDouble("rating"));
				searchResultList.add(searchResult);
			}
			return searchResultList;
		} else if (status.equalsIgnoreCase(GOOGLE_SEARCH_PLACE_STATUS.ZERO_RESULTS.status)) {
			Log.i("My-Test", "searchResultList ZERO_RESULTS = ");
		}
		return null;
	}
}